package com.android.base.dialog;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.android.base.dialog.fragment.ProgressDialogFragment;

/**
 * dialogfragment 统一管理工具类
 * Created by Administrator on 2017/1/19.
 */

public class DialogFactory {

    private final String TAG = "DialogFactory";

    private DialogFactory mDialogFactory;

    private static final String DIALOG_PROGRESS_TAG = "progress";

    private static final String DIALOG_CONFIRM_TAG = "confirm";
    private static final String DIALOG_CHARING_TAG = "charing";
    private static final String DIALOG_RQ_CODE_TAG = "rq_code";
    private static final String DIALOG_APP_UPDATE_TAG = "app_update";
    private static final String DIALOG_APP_ROUTE_TYPE = "route_type";
    private static final String DIALOG_APP_TEAM_TYPE = "team_type";
    private static final String DIALOG_APP_LOAD_TAG = "load_apk";
    private static final String DIALOG_LIST_TAG = "list";

    private FragmentManager mFragmentManager;

    public DialogFactory(FragmentManager fragmentManager) {
        mFragmentManager = fragmentManager;
    }

    /**
     * @param message    进度条显示的信息
     * @param cancelable 点击空白处是否可以取消
     */
    public void showProgressDialog(AppCompatActivity activity, String message, boolean cancelable) {
        if (mFragmentManager == null) {
            mFragmentManager = activity.getSupportFragmentManager();
        }
        try {
            /**
             * 为了不重复显示dialog，在显示对话框之前移除正在显示的对话框。
             */
            FragmentTransaction ft = mFragmentManager.beginTransaction();
            Fragment fragment = mFragmentManager.findFragmentByTag(DIALOG_PROGRESS_TAG);
            if (null != fragment) {
                ft.remove(fragment).commitAllowingStateLoss();
            }

            ProgressDialogFragment progressDialogFragment = ProgressDialogFragment.newInstance(message, cancelable);
            progressDialogFragment.show(mFragmentManager, DIALOG_PROGRESS_TAG);

            mFragmentManager.executePendingTransactions();
        } catch (Exception e) {
            Log.d(TAG, TAG + "---->" + e);
        }
    }
}
